#delimit;
clear matrix;
clear mata;
clear;
set more off;
capture log close;
estimates drop _all;
set seed 12345;

log using "C:\Users\nandi\Documents\Dropbox\My Documents - Unshared\Pop Council\Edu and health\Logfiles\nfhs5_ssa_4yrs_heterogenous.out", text replace;


use hhid hv024 hv001 hv002 sh49 hv108 hv101 using "C:\Users\nandi\Documents\Research\Data\India - NFHS (DHS)\NFHS-5\Household member\IAPR7DFL.DTA";

g temp=hv108 if hv101==1;
bysort hhid: egen headedu=sum(temp); 

drop temp hv108 hv101;

ren hv024 v024; 
ren hv001 v001; 
ren hv002 v002;

bysort hhid: keep if _n==1;
sort v024 v001 v002;

tempfile tmp;
save `tmp';


use caseid- v191a v201-v225 ml25a_6-sphase s929-s947 v007 v438 v440 v501 v714 v024 v021 v001 v002 v003 caseid v012 v133 v150 v731 v008 v104 v155 v501 v156 v714 v714a v716 v717 v731 v732 v010 v201 v213 v302a v364 v212 v511 v312 v361 v613 v602 v623 v467b v743a v743b v743d v743f v632 v604 s308y s308m d104 d107 d108 d106 v744a v744b v744c v744d v744e v312 using "C:\Users\nandi\Documents\Research\Data\India - NFHS (DHS)\NFHS-5\Individual\IAIR7DFL.DTA";

sort v024 v001 v002;
merge v024 v001 v002 using `tmp';
ta _m; drop _m;

g temp=v007*100; 
g int_yearmonth=temp+v006;

g postcovid=int_yearmont>202003;

ren v040 altitude;
ren v136 hhsize;
ren v012 age;
g agesq=age^2;
g evermarried=v501>0;
g rural=v025==2;
g urban=rural==0;

g head=(v150==1);
g wifehead=v150==2;
g childhead=v150==3;
g inlawhead=v150==4;
g gchead=v150==5;

g femalehead=v151==2;
g headage=v152;
replace headage=. if v152==.;

ren v133 eduyears;
g noedu=eduyears==0;
g belowmatric=(eduyears>0 & eduyears<10);
g highedu=(eduyears>=10);
replace highedu=. if eduyears==.;
replace belowma=. if eduyears==.;

recode sh49 (8=.);
g sc=sh49==1 & sh49~=.;
g st=sh49==2  & sh49~=.;
g obc=sh49==3  & sh49~=.;
g gencaste=sh49==4  & sh49~=.;
g scst=sc==1 | st==1;

g hindu=v130==1;
g nonhindu=(hindu~=1);
g muslim=v130==2;
g christian=v130==3;
g sikh=v130==4;

recode v155 (3=.) (4=.);
g canread=v155==2;
replace canread=. if v155==.;

gen work=v714==1;
replace work=. if v714==.;

g onekid=v201>0;
replace onekid=1 if v213==1; /***Currently pregnant**/

xtile wealthq=v190, nq(5);
tab wealthq, g(wealthq);

recode v438 (9996=.);
recode v440 (9998=.);
g HAZ=v440/100;

recode headedu (98=.);

/***
recode h7 h10 (8=.);
g vaccine=h10==1;
replace vaccine=. if h10==.;
g dpt3=h7>0 & h7<=3;
replace dpt3=. if h7==.;
***/

recode v155 (3=.) (4=.);
g literacy=v155==2;
replace literacy=. if v155==.;

recode v744a v744b v744c v744d v744e (8=.);

g emo_dv= d104==1;
replace emo_dv=. if  d104==.;

g sex_dv= d108==1;
replace sex_dv=. if  d108==.;

g severe_dv= d107==1;
replace severe_dv=. if  d107==.;

g mild_dv= d106==1;
replace mild_dv=. if  d106==.;

g physicaldv=severe_dv==1 | mild_dv==1;
replace physicaldv=. if severe_dv==.;

g emodv_12=(d103a==1 | d103a==2) | (d103b==1 | d103b==2) | (d103c==1 | d103c==2);
replace emodv_12=. if d103a==.;

g severe_12months=(d105d==1 | d105d==2) | (d105e==1 | d105e==2) | (d105f==1 | d105f==2);
replace severe_12months=. if d105d==.;

g mild_12months=(d105a==1 | d105a==2) | (d105b==1 | d105b==2) | (d105c==1 | d105c==2) | (d105j==1 | d105j==2) ;
replace mild_12months=. if d105a==.;

g physicaldv_12=severe_12months==1 | mild_12months==1;
replace physicaldv_12=. if severe_12months==.;

g sexdv_12months=(d105h==1 | d105h==2) | (d105i==1 | d105i==2) | (d105k==1 | d105k==2) ;
replace sexdv_12months=. if d105h==.;


/*****
recode m19 9998=.;

g lbw=m19<2500;
replace lbw=. if m19==.;
****/

g market=s930a==1;
replace market=. if s930a==.;

g hf=s930b==1;
replace hf=. if s930b==.;

g outofvill=s930c==1;
replace outofvill=. if s930c==.;

g dv_cook=v744e==1;
replace dv_cook=. if v744e==.;

g dv_neglect=v744b==1;
replace dv_neglect=. if v744b==.;

g dv_argue=v744c==1;
replace dv_argue=. if v744c==.;

g dv_outside=v744a==1;
replace dv_outside=. if v744a==.;

g usemoney=v743f==1 | v743f==2;
replace usemoney=. if v743f==.;

g notrust_money=d101f==1;
replace notrust=. if d101f==.;

g age2001=age-(v007-2001);
g ssa=(age2001<=14);

g wealthq12=wealthq1==1 | wealthq2==1;
g wealthq45=wealthq4==1 | wealthq5==1;
g noeduhead=headedu==0;


g phy_dv=0;
replace phy_dv=1 if mild_dv==1 | severe_dv==1;
replace phy_dv=. if mild_dv==. | severe_dv==.;


keep if age2001>=11 & age2001<=18;
keep if everma==1;

global xvar "age agesq head wifehead childhead inlawhead gchead headage femalehead headedu hhsize rural sc st obc muslim christian sikh wealthq2 wealthq3 wealthq4 wealthq5"; 

lab var age "Age";
lab var agesq "Age squared";
lab var eduyears "Years of schooling";
lab var head "Whether head of the household";
lab var wifehead "Household head's spouse";
lab var childhead "Household head's child";
lab var inlawhead "Household head's daughter-in-law";
lab var gchead "Household head's grandchild";
lab var femalehead "Household head is female";
lab var headage "Household head's age";
lab var headedu "Household head's years of schooling";
lab var hhsize "Household size";
lab var rural "Rural household";
lab var sc "Scheduled Caste household";
lab var st "Scheduled Tribe household";
lab var obc "Other Backward Classes household";
lab var muslim "Muslim household";
lab var christian "Christian household";
lab var sikh "Sikh household";
lab var wealthq2 "Wealth quintile 2";
lab var wealthq3 "Wealth quintile 3";
lab var wealthq4 "Wealth quintile 4";
lab var wealthq5 "Wealth quintile 5";


foreach var of varlist rural urban noedu belowmatric highedu gencaste scst obc wealthq12 wealthq45{;

preserve;
keep if `var'==1;
g rand=runiform();
sort rand;

di in red "Subgroup: `var'";

*xi: psmatch2 ssa $xvar, outcome(dv_*) kernel common qui;
xi: psmatch2 ssa $xvar, outcome(emodv_12 physicaldv_12) kernel common qui;



restore;
};

log close;